# 图书管理系统

# 功能简介
# 添加图书时，图书ID不能重复，图书名可重复
# 删除，查询，修改功能，输入图书名之后提供所有的同名的图书，用户可以按照图书序号对具体的一本书进行操作
# 显示书籍，分行显示，每行一本书的信息

# 显示菜单
import json

book_list = []


# 书本对象
class book:

    # 初始化字段
    def __init__(self, book_id, book_name, book_location):
        self.book_id = book_id
        self.book_name = book_name
        self.book_location = book_location

    def __str__(self):
        return f"book_id={self.book_id},book_name={self.book_name}," \
               f"book_location={self.book_location}"


def print_muen():
    print("----菜单提示----")
    print("[1]:添加图书")
    print("[2]:查询图书")
    print("[3]:删除图书")
    print("[4]:修改图书信息")
    print("[5]:显示所有图书")
    print("[6]:退出")
    select = eval(input("请选择操作:\n"))
    return select


# 添加图书
def add_book(book_name, book_location):
    # 先判断位置是否存在书籍  filter(lambda x: str(x).isdigit(), str_list)
    is_exist = filter(lambda x: x.book_location == book_location, book_list)
    if list(is_exist):
        print(f"该位置已经存在图书")
        return
        # raise Exception(f"该位置已经存在图书:{list(is_exist)[0].book_name}")
    book_list.sort(key=lambda x: x.book_id, reverse=True)
    book_id = 1
    if len(book_list) > 0:
        last_book = book_list[len(book_list) - 1]
        book_id = last_book.book_id + 1
    b = book(book_id, book_name, book_location)
    book_list.append(b)
    print(f"添加图书成功!!!,图书信息:{json.dumps(b, default=lambda obj: obj.__dict__, ensure_ascii=False)}")


# 查询图书
def query_book(book_id):
    if len(book_list) <= 0:
        print("当前没有书籍,去添加试试吧")
        return
    is_exist = filter(lambda x: x.book_id == int(book_id), book_list)
    is_exist_list = list(is_exist)
    if len(is_exist_list) < 1:
        print("该图书不存在")
        return
        # raise Exception(f"该图书不存在")
    print("%s\t%s\t\t%s" % (is_exist_list[0].book_id,
                            is_exist_list[0].book_name, is_exist_list[0].book_location))


# 修改图书
def update_book(book_id, book_name, book_location):
    if len(book_list) <= 0:
        print("当前没有书籍,去添加试试吧")
        return
    is_exist = filter(lambda x: x.book_id == int(book_id), book_list)
    is_exist_list = list(is_exist)
    if len(is_exist_list) < 1:
        print("该图书不存在")
        return
        # raise Exception(f"该图书不存在")
    book_info = is_exist_list[0]
    if book_name:
        book_info.book_name = book_name
    if book_location:
        book_info.book_location = book_location
    print(f"修改图书成功!!!,图书信息:{json.dumps(book_info, default=lambda obj: obj.__dict__, ensure_ascii=False)}")


def del_book(book_id):
    if len(book_list) <= 0:
        print("当前没有书籍,去添加试试吧")
        return
    is_exist = filter(lambda x: x.book_id == int(book_id), book_list)
    is_exist_list = list(is_exist)
    if len(is_exist_list) < 1:
        # raise Exception(f"该图书不存在")
        print("该图书不存在")
        return
    for val in range(len(book_list), 0, -1):
        b = book_list[val - 1]
        if b.book_id == int(book_id):
            del book_list[val - 1]
            print(f"删除图书成功!!!,图书信息:{json.dumps(b, default=lambda obj: obj.__dict__, ensure_ascii=False)}")


# 展示图书
def show_book():
    if len(book_list) <= 0:
        print("当前没有书本,去添加试试吧")
    else:
        # print("书籍id\t书籍名称\t位置")
        for index, b in enumerate(book_list):
            print("%s\t%s\t\t%s" % (b.book_id,
                                    b.book_name, b.book_location))


manager_list = [{"name": "张大力", "password": 123456}, {"name": "王大陆", "password": 234567}]


# 登录方法
def login(name, password):
    # 查询教师信息
    teach = manager_is_exists(name)
    if not teach:
        print("账户不存在,请联系管理员!")
    else:
        if teach.get("password") == int(password):
            return 0
        else:
            print("密码错误,请重新输入!")
    return -1


# 判断账户是否存在
def manager_is_exists(name):
    for teach in manager_list:
        if teach.get("name").__eq__(name):
            return teach
    return None


def init():
    name = input("请输入账号:")
    password = input("请输入密码:")
    if login(name, password) == 0:
        while True:
            # 显示菜单
            select = print_muen()
            if select == 1:
                book_name = input("请输入书本名称:")
                book_location = input("请输入书本位置:")
                add_book(book_name, book_location)
            elif select == 2:
                book_id = input("请输入书本id:")
                query_book(book_id)
            elif select == 3:
                book_id = input("请输入书本id:")
                del_book(book_id)
            elif select == 4:
                book_id = input("请输入书本id:")
                book_name = input("请输入书本名称:")
                book_location = input("请输入书本位置:")
                update_book(book_id, book_name, book_location)
            elif select == 5:
                show_book()
            elif select == 6:
                print("----------Bye----------")
                return
            else:
                print("输入指令有误请重新输入!")

    else:
        init()


init()
